package org.opens.magicByte;

import com.github.misterchangray.core.MagicByte;
import com.github.misterchangray.core.annotation.MagicClass;
import com.github.misterchangray.core.annotation.MagicField;
import lombok.Data;
import org.opens.util.ByteUtil;

@MagicClass()
@Data
public class SatelliteAndMissileFrames {

    /**
     * 同步头
     * 长度: 4Byte
     */
    @MagicField(order = 1, size = 8)
    private String synchronizationHead;

    /**
     * 帧长
     * 长度: 2Byte
     */
    @MagicField(order = 2, size = 4)
    private String frameLength;

    /**
     * 帧长hec
     * 长度: 2Byte
     */
    @MagicField(order = 3, size = 4)
    private String frameLengthHec;

    /**
     * 目的站地址
     * 长度: 16Bit
     */
    @MagicField(order = 4, size = 4)
    private String destinationAddress;

    /**
     * 源站地址
     * 长度: 16Bit
     */
    @MagicField(order = 5, size = 4)
    private String originAddress;

    /**
     * 帧类型
     * 长度: 6Bit
     */
    @MagicField(order = 6, size = 3)
    private String frameType;

    /**
     * 优先级
     * 长度: 4Bit
     */
    @MagicField(order = 7, size = 2)
    private String priority;

    /**
     * 保留
     * 长度: 6Bit
     */
    @MagicField(order = 8, size = 3)
    private String hold;

    /**
     * 保密字段
     * 长度: 32Byte
     */
    @MagicField(order = 9, size = 64)
    private String confidentialField;

    /**
     * 净荷
     * 长度: 112Byte
     */
    @MagicField(order = 10, size = 224)
    private String netLoad;

    /**
     * 帧crc
     * 长度: 2Byte
     */
    @MagicField(order = 11, size = 4)
    private String frameCrc;

    // frameType默认2进制值
    private static final String FRAME_TYPE_DEFAULT_BINARY = "100101";
    // priority默认2进制值
    private static final String PRIORITY_DEFAULT_BINARY = "0011";
    // hold默认2进制值
    private static final String HOLD_DEFAULT_BINARY = "000000";

    public SatelliteAndMissileFrames() {
        this.synchronizationHead = "ACFFC1D";
        this.frameLength = "9C";
        this.destinationAddress = "CFC1";
        this.originAddress = "57E4";
        this.confidentialField = "673C4CCB0C3F96ED16CE529548E68576D623DB720876015D77B368201338D04C";
        this.frameType = ByteUtil.binaryStringToHexString(FRAME_TYPE_DEFAULT_BINARY);
        this.priority = ByteUtil.binaryStringToHexString(PRIORITY_DEFAULT_BINARY);
        this.hold = ByteUtil.binaryStringToHexString(HOLD_DEFAULT_BINARY);
        this.frameCrc = "CFC1";
    }

    public String convertToStringFrame() {
        byte[] bytes = MagicByte.unpackToByte(this);
        return ByteUtil.byteArrayToHexString(bytes);
    }

}
